#include "ttableview.h"
#include <QMouseEvent>
#include "common/tdebug.h"
#include <QScrollBar>
#include <QDate>
#include <QTimer>


#define MOVE_S         5.00 //每次移动距离
#define ACCEL_SPEED    2
#define ACCEL_INTERVAL 50
#define MOVE_MIN       3


#define A  0.01
TTableView::TTableView(QWidget *parent) :
    QTableView(parent)
{
    mBar = this->verticalScrollBar();
//    mBar->setMaximum(rect().bottom());
//    mBar->setMinimum(rect().top());

    mScrollValid = false;
    QHeaderView *hHead = this->horizontalHeader();
    QHeaderView *vHead = this->verticalHeader();

    setShowGrid(true);
    //hHead->hide();
    vHead->hide();
//#if QT_VERSION >= 0X50000
//    hHead->setSectionResizeMode(QHeaderView::Stretch);
//#else
//    hHead->setResizeMode(QHeaderView::Stretch);
//    //hHead->setResizeMode(QHeaderView::ResizeToContents);
//#endif

    setHorizontalScrollMode(QAbstractItemView::ScrollPerItem);
    mPerPageRow = 10;
    connect(mBar,SIGNAL(valueChanged(int)),this,SLOT(onPageChanged(int)));
}



void TTableView::setScrollVaild(bool scrollValid)
{
    mScrollValid = scrollValid;
}


/*
void TTableView::mouseMoveEvent(QMouseEvent *event)
{

    if(mScrollValid)
    {
        mOffsetY =  event->y()-mPressPosY;

        int offsetY = (mOffsetY*mBar->pageStep()+h/2)/h;
        mBar->setValue(mScrollValue -offsetY);
    }
    else
        QTableView::mouseMoveEvent(event);


}*/


void TTableView::mousePressEvent(QMouseEvent *event)
{
    canLongPress = true;
    QTimer::singleShot(1500,this,SLOT(longPress()));
    QTableView::mousePressEvent(event);
    mEvent = event;
//    if(mScrollValid)
//    {
//        mOffsetY = 0;
//        mPressPosY = event->y();
//        mScrollValue = mBar->value();
//    }

}



void TTableView::mouseReleaseEvent(QMouseEvent *event)
{
    if(canLongPress)
    {
       canLongPress = false;
       emit shortPressed(this->currentIndex());
       qDebug()<<"short pressed.....";


    }
    else
    {



    }
     QTableView::mouseReleaseEvent(event);
//    if(mScrollValid)
//    {
//        if(mOffsetY>MOVE_MIN||mOffsetY<-MOVE_MIN)
//        {
//            canLongPress = false;
//            return;
//        }
//    }
}

void TTableView::longPress()
{
    if(canLongPress)
    {
        canLongPress = false;
        QTableView::mouseReleaseEvent(mEvent);
        emit longPressed(this->currentIndex());


    }
}


bool TTableView::isOnBottom()
{
    if(mBar->value()>=mBar->maximum())
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool TTableView::isOnTop()
{
    if(mBar->value()<=mBar->minimum())
    {
        return true;
    }
    else
    {
        return false;
    }
}


void TTableView::setPerPageRow(int n)
{
    mPerPageRow = n;
}

QModelIndexList TTableView::selectedIndexes() const
{
    return QTableView::selectedIndexes();
}

void TTableView::setHHeadResizeMode(QHeaderView::ResizeMode mode)
{
    if(mode<0)
    {}
    else
    {
#if QT_VERSION >= 0X50000
        this->horizontalHeader()->setSectionResizeMode(mode);
#else
        this->horizontalHeader()->setResizeMode(mode);
#endif
    }
}

void TTableView::showEvent(QShowEvent *e)
{

    if(mPerPageRow!=0){
        this->verticalHeader()->setDefaultSectionSize((this->height())/mPerPageRow);
    }
    mBar->setRange(0,(mBar->maximum()+mBar->pageStep())-mPerPageRow);
    mBar->setPageStep(mPerPageRow);
    QTableView::showEvent(e);
    emit pageChanged(currentPage(),pages());

}


//void TTableView::paintEvent(QPaintEvent *e)
//{

//    if(mPerPageRow!=0){
//        this->verticalHeader()->setDefaultSectionSize((this->height())/mPerPageRow);
//    }
//    mBar->setRange(0,(mBar->maximum()+mBar->pageStep())-mPerPageRow);
//    mBar->setPageStep(mPerPageRow);
//    QTableView::paintEvent(e);

//}

void TTableView::pageUp()
{
    mBar->triggerAction(QAbstractSlider::SliderPageStepSub);
   // mBar->setValue(mBar->value()-mBar->pageStep());

}

void TTableView::pageDown()
{
    mBar->triggerAction(QAbstractSlider::SliderPageStepAdd);
    // mBar->setValue(mBar->value()+mBar->pageStep());
}

void TTableView::onPageChanged(int value)
{
    emit pageChanged(currentPage(),pages());
}



int TTableView::pages()
{
    //qDebug()<<mBar->maximum()<<mBar->minimum()<<mBar->pageStep();
    return ((mBar->maximum()-mBar->minimum())+mBar->pageStep()-1)/(mBar->pageStep())+1;
}

int TTableView::currentPage()
{
    return ((mBar->value()-mBar->minimum())+mBar->pageStep()-1)/(mBar->pageStep())+1            ;
}

/*
void MoveTableView::timerEvent(QTimerEvent *event)
{
    if(event->timerId()==mMoveTimerId)
    {

        mScrollValue -=mSpeed;
        mBar->setValue(mScrollValue);
        if(mScrollValue<=0||mScrollValue>=mBar->maximum())
        {
             killTimer(mMoveTimerId);
             return;
        }

        if(mSpeed<0)
        {
            mSpeed+=ACCEL_SPEED;
            if(mSpeed>=0)
                killTimer(mMoveTimerId);
        }
        else
        {
            mSpeed-=ACCEL_SPEED;
            if(mSpeed<=0)
                killTimer(mMoveTimerId);
        }
        //////tDebug()<<mSpeed;


    }
    QTableView::timerEvent(event);
}

*/
